package com.cn.bulu.service.impl;

import com.cn.bulu.constant.CurtainConstant;
import com.cn.bulu.dao.CustomerDao;
import com.cn.bulu.dao.GoodsDao;
import com.cn.bulu.dao.IndentDao;
import com.cn.bulu.dao.UserDao;
import com.cn.bulu.pojo.Customer;
import com.cn.bulu.pojo.Indent;
import com.cn.bulu.pojo.User;
import com.cn.bulu.service.IndentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IndentServiceImpl implements IndentService {

	@Autowired 
	private IndentDao indentDao;
	@Autowired
	private CustomerDao customerDao;
	@Autowired
	private UserDao userDao;
	@Autowired
	private GoodsDao goodsDao;
	
	
	@Override
	public List<Map<String,Object>> showIndents(Integer page) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<>();
		map.put("page", (page - 1) * 10);
		map.put("pageSize", CurtainConstant.PAGESIZE);
		return indentDao.selectAllIndents(map);
	}
	
	
	public int getTotalPages(){
		int totalRecords=indentDao.getAllRecords();
		//int totalPageNum = (totalRecords  +  CurtainConstant.PAGESIZE  - 1) / CurtainConstant.PAGESIZE;  
		return totalRecords;
	}
	
	
	
	@Override
	public List<Map<String,Object>> showGoods(Integer indentId) {
		// TODO Auto-generated method stub
		return indentDao.selectGoodsOfMine(indentId);
	}
	
	
	/**
	 * 功能：下单人是顾客的话
	 * @param customer
	 * @return
	 */
	public User isReferred(Customer customer){
		if(customer==null)return null;
		String phone=customer.getPhone();
		if(!StringUtils.isEmpty(phone)){
			Integer userId=userDao.selectUserByCustomer(phone);
			if(userId!=null)
				return userDao.selectUserById(userId);
		}
		return null;
	}
	
	/**
	 * 功能：指定商品对应的各级代理利润率
	 * @param vertification
	 * @param firstProfit
	 * @param fSProfit
	 * @param secondProfit
	 * @return
	 */
	public Map<String,Object> getProfitRate(String vertification,Integer goodsId,Double firstProfit,Double fSProfit,Double secondProfit){
		Map<String,Object> map=new HashMap<>();
		Double profitRateAgent=0.00;
		Double profitRateDeclar=0.00;
		Double profitRatePromotor=0.00;
		switch (vertification) {
		case "1"://下单人是代理商
			profitRateAgent=firstProfit+fSProfit+secondProfit;
			break;
		case "2"://下单人是经销商
			profitRateAgent=firstProfit;
			profitRateDeclar=fSProfit+secondProfit;
			break;
		case "3"://下单人是推广大使
			profitRateAgent=firstProfit;
			profitRateDeclar=fSProfit;
			profitRatePromotor=secondProfit;
			break;
		default:
			break;
		}
		map.put("goodsId", goodsId);
		map.put("profitRateAgent", profitRateAgent);
		map.put("profitRateDeclar", profitRateDeclar);
		map.put("profitRatePromotor", profitRatePromotor);
		return map;
	}
	
	/**
	 * 功能：顾客下单后各级代理分配利润率
	 * @param indentId
	 */
	@Override
	public  void updateProfitOfAgentors(Integer indentId){
		Indent indent =indentDao.selectIndentById(String.valueOf(indentId));
		if(indent !=null && indent.getStatus() !=4){
			Customer customer=customerDao.getCustomerById(indent.getCustomerId());//下单人
			if(customer !=null){
				List<Map<String,Object>> goodsList=showGoods(indentId);
				for(Map<String,Object> goodMap : goodsList){
					Double firstProfit=Double.valueOf(String.valueOf(goodMap.get("firstProfit")));
					Double fSProfit=Double.valueOf(String.valueOf(goodMap.get("fSProfit")));
					Double secondProfit=Double.valueOf(String.valueOf(goodMap.get("secondProfit")));
					Map<String,Object> profitsMap=null;
					if(!("0".equals(customer.getVertification()))){//代理商，经销商，或者推广大使
						profitsMap=getProfitRate(customer.getVertification(),Integer.valueOf(String.valueOf(goodMap.get("goodsId"))),firstProfit,fSProfit,secondProfit);
					}else{//普通顾客
						//判断是否为代理推荐
						User user=isReferred(customer);
						if(user !=null){
							profitsMap=getProfitRate(user.getVertification(),Integer.valueOf(String.valueOf(goodMap.get("goodsId"))),firstProfit,fSProfit,secondProfit);
						}
					}
					//修改利润
					if(profitsMap !=null && ! profitsMap.isEmpty()){
						goodsDao.updateGoodsProfit(profitsMap);
					}
				}
			}
		}
	}
}
